define(function(require, exports) {
	var pubjs = require('pubjs');
	var util = require('util');
	var view = require('@base/view');
	var $ = require('jquery');
	var Drop = require('@base/common/dropdown').base;

	var channels = util.clone(pubjs.config('exchange'));
	var channelMap = {};
	util.each(channels, function(item)
	{
		channelMap[item.id] = item;
		// todo资质审核状态呆确定
		// item.qualification_status = '';
	});

	var SEN_PREFIX = 'sensitive_';
	var IND_PREFIX = 'industry_';

	var Channel = view.container.extend({
		init: function(config) {
			config = pubjs.conf(config, {
				class: 'P-campaignEditChannel',
				vModel: {
					'channels': pubjs.config('exchange'),
					'selects': [],
					'data': []
				},
				vMethods: {
					'evChannel': this.evChannel
				},
				vHooks: {
					afterAppend: this.afterAppend,
					beforeRemove: this.beforeRemove
				}
			});

			this.$index_to_channel = {};

			this.Super('init', arguments);
		}
		,afterBuild: function() {
			var self = this;
			var el = self.getDOM();
			el.css('min-height', 400);

			$([
				'<div>' + LANG('广告渠道：') + '</div>',
				'<div>',
					'<span class="channel-name"',
						'v-for="item in channels" v-on:click="evChannel(item.id, item)"',
						'v-bind:class="{select: (selects.indexOf(item.id) != -1)}"',
						'v-bind:title="item.name">',
						'{{item.name}}',
					'</span>',
				'</div>',
				'<div class="category">',
					'<div>' + LANG('已选渠道：') + '</div>',
					'<div class="M-tableList">',
						'<table class="table">',
							'<thead>',
								'<tr>',
									'<th>渠道</th>',
									'<th>行业分类</th>',
									'<th>敏感分类</th>',
									'<th>资质审核状态</th>',
								'</tr>',
							'</thead>',
							'<tbody>',
								'<tr v-for="item in data" v-hook:after="afterAppend" v-hook:before="beforeRemove">',
									'<td>{{item.name}}</td>',
									'<td class="industry"></td>',
									'<td class="sensitive"></td>',
									'<td>{{item.qualification}}</td>',
								'</tr>',
							'</tbody>',
						'</table>',
					'</div>',
				'</div>'
			].join('')).appendTo(el);

			self.buildFromTemplate({

			});


			self.vm.mount();
		}
		,afterBuildTemplate: function()
		{

		}
		,reset: function()
		{
			this.vm.reset();
			return this;
		}
		,getValue: function()
		{

		}
		,evChannel: function(channel_id, item)
		{
			var vm_data = this.vm.$data;
			var index = vm_data.selects.indexOf(channel_id);
			if (index == -1)
			{
				vm_data.selects.push(channel_id);
				vm_data.data.push(util.clone(channelMap[channel_id]));
			}
			else
			{
				vm_data.selects.splice(index, 1);
				vm_data.data.splice(index, 1);
			}
		}
		,afterAppend: function(el, index)
		{
			var cs = this.$;
			var vm_data = this.vm.getCopy();
			var item = vm_data.data[index];
			this.$index_to_channel[index] = item.id;
			// 各个渠道的元素
			var sensitive_el = $(el).find('.sensitive');
			var industry_el = $(el).find('.industry');
			// 各个渠道模块的名称
			var industry_name = IND_PREFIX + item.id;
			var sensitive_name = SEN_PREFIX + item.id;

			var industry_data = item.industry_data;
			var sensitive_data = item.sensitive_data;

			var mod;
			// 敏感分类
			if (item.campaign_sensitive)
			{
				mod = this.create(sensitive_name, Drop, {
					'reqType': 'websocket',
					'url': 'category/sensitive',
					'param': {
						'ChannelId': item.id
					},
					'target': sensitive_el,
					'appendTo': 'popup'
				});
				if (sensitive_data)
				{
					mod.setData(sensitive_data);
				}
			}

			// 行业分类
			if (item.campaign_industry)
			{
				mod = this.create(industry_name, Drop, {
					'reqType': 'websocket',
					'url': 'category/industry',
					'param': {
						'ChannelId': item.id
					},
					'target': industry_el,
					'appendTo': 'popup'
				});
				if (industry_data)
				{
					mod.setData(industry_data);
				}
			}

			// todo 资质状态
		}
		,beforeRemove: function(el, index)
		{
			var cs = this.$;
			var vm_data = this.vm.getCopy();
			var channel_id = this.$index_to_channel[index];
			var channel_info = channelMap[channel_id];
			var industry = cs[IND_PREFIX + channel_id];
			var sensitive = cs[SEN_PREFIX + channel_id];

			if (channel_info.campaign_sensitive && sensitive)
			{
				sensitive.destroy();
			}

			if (channel_info.campaign_industry && industry)
			{
				industry.destroy();
			}

			this.$index_to_channel[index] = null;

			util.each(vm_data.data, function(item, index)
			{
				this.$index_to_channel[index] = item.id;
			}, this);
		},
		setData: function(items)
		{
			var vm_data = this.vm.$data;
			var ret = [];
			var ids = [];
			util.each(items, function(item)
			{
				var channel_id = item.channel_id;
				var tmp = util.clone(channelMap[channel_id]);
				tmp.industry_data = item.industry_data;
				tmp.sensitive_data = item.sensitive_data;
				ret.push(tmp);
				ids.push(channel_id);
			}, this);
			// 所选的不同渠道ids，不同类型的行业分类敏感分类详细数据
			vm_data.selects = ids;
			vm_data.data = ret;
		},
		getData: function()
		{
			var cs = this.$;
			var items = [];
			var vm_data = this.vm.getCopy();

			// 根据所选的不同渠道，获取不同类型的行业分类敏感分类数据
			util.each(vm_data.selects, function(channel_id)
			{
				var tmp = {};
				var industry = cs[IND_PREFIX + channel_id];
				var sensitive = cs[SEN_PREFIX + channel_id];

				tmp.channel_id = channel_id;
				if (industry)
				{
					tmp.industry_data = industry.getData();
				}

				if (sensitive)
				{
					tmp.sensitive_data = sensitive.getData();
				}

			}, this);
		}
	});
	exports.base = Channel;

});